package org.imaginedays.androidall.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.imaginedays.androidall.R;
import org.imaginedays.androidall.app.BaseActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class DisplayTextOnScreenACT extends BaseActivity {

	// all/data.txt
	private static final String FILENAME = "data.txt";

	private TextView tv_result;
	private EditText et_entry;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.display_text_on_screen);

		tv_result = (TextView) findViewById(R.id.tv_result);
		et_entry = (EditText) findViewById(R.id.et_input);

	}

	public void start(View view) {

		String content = et_entry.getText().toString();

		// 输出流
		try {
			FileOutputStream mOutPut = openFileOutput(FILENAME,
					Context.MODE_PRIVATE);
			mOutPut.write(content.getBytes());
			mOutPut.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		// 输入流
		try {
			FileInputStream mInput = openFileInput(FILENAME);
			byte[] data = new byte[1024];
			mInput.read(data);
			mInput.close();

			String display = new String(data);
			tv_result.setText(display);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
